package model

import (
	"gitee.com/liumou_site/gcs"
	"gitee.com/liumou_site/gf"
)

func MkdirRun() error {
	run := gcs.NewShell()
	d := "/run/sshd"
	// 判断文件夹是否存在
	f := gf.NewFile(d)
	f.IsDir()
	f.Exists()
	if f.ExIst {
		if !f.IsDirs {
			// 不是文件夹，删除
			GetLanguageManager().Warn("目标路径已存在，但不是文件夹，删除: ", "Target path already exists but is not a directory, deleting: ", d)
			f.DeleteDir()
		}
		f.Mkdir(f.FileName, 0755)
		if f.Err != nil {
			GetLanguageManager().Error("创建文件夹失败: ", "Failed to create directory: ", d)
			return f.Err
		}
	}
	f.IsDir()
	f.Exists()
	if !f.ExIst {
		run.RunShell("mkdir", "-p", d)
		if run.Err != nil {
			GetLanguageManager().Error("创建文件夹失败: ", "Failed to create directory: ", d)
			GetLanguageManager().Debug(run.Text, run.Text)
			return run.Err
		}
	}
	// 设置属主
	run.RunShell("chown", "root:root", d)
	if run.Err != nil {
		GetLanguageManager().Error("设置文件夹属主失败: ", "Failed to set directory owner: ", d)
		GetLanguageManager().Debug(run.Text, run.Text)
		return run.Err
	}
	return nil
}
